package com.gxc.datacore.ws.wsservice;

import java.util.Date;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import com.gxc.datacore.query.service.DataQueryService;
import com.gxc.datacore.ws.domain.RequestInfo;
import com.gxc.datacore.ws.domain.ResponseInfo;
import com.gxc.datacore.ws.util.UserAccessUtil;
import com.gxc.datacore.ws.util.XmlUtil;
import com.gxc.webcore.util.ContextUtil;
import com.gxc.webcore.util.StringUtil;

/**
 * 数据查询的webservice
 * 
 * @author gxc
 * 
 */
@WebService(targetNamespace = "http://ws.datacenter.gxc.com")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class QueryService {

	/**
	 * 数据查询webservice方法
	 * 
	 * @param requestXml
	 * @return responseXml
	 */
	@SuppressWarnings("rawtypes")
	@WebMethod(action = "query", operationName = "query", exclude = false)
	@WebResult(name = "responseXml")
	public String query(@WebParam(name = "requestXml") String requestXml) {
		RequestInfo requestInfo = null;
		ResponseInfo responseInfo = new ResponseInfo();
		responseInfo.setId(StringUtil.getUUID());

		// 解析requestXml
		try {
			requestInfo = XmlUtil.parseRequestXml(requestXml);
			responseInfo.setRequest_id(requestInfo.getId());
		} catch (Exception e) {
			// requestXml格式不正确，解析失败。
			responseInfo.setStatus_code("");
			responseInfo.setStatus_name("requestXml格式不正确，解析失败。");
			return XmlUtil.buildResponseXml(responseInfo);
		}

		// 校验用户名密码
		boolean flag = UserAccessUtil.checkPassword(requestInfo.getUsername(),
				requestInfo.getPassword());
		// 查询
		if (flag) {
			List resultList = null;
			try {
				DataQueryService dataQueryService = (DataQueryService) ContextUtil
						.getBean(DataQueryService.class);
				resultList = dataQueryService.query(requestInfo.getUsername(),
						requestInfo.getObject_id(), requestInfo.getParams(),
						true, 0, 20);
			} catch (Exception e) {
				// 查询失败。
				responseInfo.setStatus_code("");
				responseInfo.setStatus_name("查询失败。");
				responseInfo.setResponse_time(new Date());
				return XmlUtil.buildResponseXml(responseInfo);
			}
			if (resultList == null) {
				// 没有该数据表的访问权限
				responseInfo.setStatus_code("");
				responseInfo.setStatus_name("没有该数据表的访问权限。");
			} else if (resultList.size() == 0) {
				// 没有查询到数据
				responseInfo.setStatus_code("");
				responseInfo.setStatus_name("没有查询到数据。");
			} else {
				// 查询成功
				responseInfo.setStatus_code("");
				responseInfo.setStatus_name("查询成功。");
				responseInfo.setResult(resultList);
			}
			responseInfo.setRequest_id(requestInfo.getId());
			responseInfo.setResponse_time(new Date());
			return XmlUtil.buildResponseXml(responseInfo);
		} else {
			// 权限校验不通过。
			responseInfo.setStatus_code("");
			responseInfo.setStatus_name("用户名或密码不正确。");
			responseInfo.setResponse_time(new Date());
			return XmlUtil.buildResponseXml(responseInfo);
		}
	}

}
